<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
	报餐
*/
class Order extends Base_Controller{
	
	private $userid;
	private $openid;

	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->library('Wework/wework', [
			'corpsecret' => 'pjQrOogM4feQ_oqbnZ697Civt5kGR7P3dAdUCFo-O1Y'
		]);
		$this->openid = $this->input->cookie('food_openid');
		$this->load->model('food_order_model');
		// $this->openid = 'o4pov1HCYlQ8qDPlym2t1WKY1Zmc';
	}
	
	public function index(){
		if(!$this->openid){
			$this->get_openid();
			$userid = $this->userid;
		}else{
			$userid = $this->wework->openid_to_userid($this->openid)['userid'];
		}
		if(!$userid){
			$this->show_message('非企业成员');
		}
		$user = $this->wework->user_get($userid);
		if(!$user){
			$this->show_message('用户信息获取失败');
		}
		if($user['status'] !== 1){
			$this->show_message('无效用户，请联系管理员');
		}
		$this->session->set_userdata('food_user', $user);
		$order = $this->today_order($this->openid);
		
		// 今日可就餐
		$news = $this->food_order_model->get_row([
			'openid' => $this->openid,
			'validay' => strtotime(date('Y-m-d'))
		]);
		$today_meal = [];
		if($news){
			if($news['breakfast'] == 1){
				$today_meal[] = '早餐';
			}
			if($news['lunch'] == 1){
				$today_meal[] = '中餐';
			}
			if($news['dinner'] == 1){
				$today_meal[] = '晚餐';
			}
		}
		if($today_meal){
			$today_meal = implode(' , ', $today_meal);
		}else{
			$today_meal = '无';
		}
		
		$assign = [
			'user' => $user,
			'order' => $order,
			'today_meal' => $today_meal,
			'valid_time' => (int)$this->valid_order_time()
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('order_index.htm');
	}
	
	// 报餐提交
	public function submit(){
		$posts = $this->input->post();
		if(!$posts){
			$this->output_json();
		}
		$user = $this->session->userdata('food_user');
		$openid = $this->openid;
		if(!$user || !$openid){
			$this->output_json('登录已失效，请重新打开');
		}
		if(!$this->valid_order_time()){
			$this->output_json('请在规定时间内报餐');
		}
		$row = $this->today_order($openid);
		$department = $this->wework->department_list($user['main_department']);
		$data = [
			'name' => $user['name'],
			'sex' => $user['gender'] == 1 ? '男' : '女',
			'department' => $department['department'][0]['name'],
			'mobile' => $user['mobile'],
			'breakfast' => $posts['breakfast'],
			'lunch' => $posts['lunch'],
			'dinner' => $posts['dinner'],
			'updatetime' => time()
		];
		if($row){
			$query = $this->food_order_model->set($data, [
				'id' => $row['id'],
				'openid' => $openid
			]);
		}else{
			$data['openid'] = $openid;
			$nextday = date('Y-m-d', strtotime('+1 day'));
			$data['validay'] = strtotime($nextday);
			$data['createtime'] = time();
			$query = $this->food_order_model->set($data);
		}
		if($query){
			$this->output_json('提交成功', 0);
		}
		$this->output_json('提交失败');
	}
	
	// 核销
	public function check($action = ''){
		$this->load->helper('common');
		$token = $this->input->get_post('token');
		
		$params = secret_char($token, false);
		$params = json_decode($params, true);
		if(!$token || $params['sign'] != mmd5($params['validtime'])){
			$this->show_message('页面参数异常');
		}
		
		$nowtime = time();
		if($nowtime > $params['validtime']){
			$this->show_message('二维码已失效，请重新扫描');
		}
		
		$time1_min = strtotime(date('Y-m-d 06:50:00'));
		$time1_max = strtotime(date('Y-m-d 08:00:00'));
		$time2_min = strtotime(date('Y-m-d 11:30:00'));
		$time2_max = strtotime(date('Y-m-d 12:45:00'));
		$time3_min = strtotime(date('Y-m-d 17:30:00'));
		$time3_max = strtotime(date('Y-m-d 18:30:00'));
		if($nowtime >= $time1_min && $nowtime <= $time1_max){
			$type = 'breakfast';
		}else if($nowtime >= $time2_min && $nowtime <= $time2_max){
			$type = 'lunch';
		}else if($nowtime >= $time3_min && $nowtime <= $time3_max){
			$type = 'dinner';
		}else{
			$this->show_message('请在规定时间内核销');
		}
		$subject = $this->food_order_model->types($type);
		$timefield = $type.'time';
		
		if(!$this->openid){
			$this->get_openid();
		}
		$nowday = strtotime(date('Y-m-d'));
		$row = $this->food_order_model->get_row([
			'openid' => $this->openid,
			'validay' => $nowday
		]);
		if(!$row || !$row[$type]){
			$this->show_message('未查询到您的报餐信息');
		}
		$checked = false;
		if($row[$type] != 1){
			$checked = true;
		}
		if('submit' == $action){
			if($checked){
				$this->show_message('已经核销过了');
			}
			$query = $this->food_order_model->set([
				$timefield => time(),
				$type => 2
			], [
				'id' => $row['id'],
				'openid' => $this->openid
			]);
			if($query){
				$this->show_message('核销成功', 0);
			}
			$this->show_message('核销失败');
		}
		$assign = [
			'datestr' => date('n月d日'),
			'subject' => $subject,
			'checked' => $checked
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('order_check.htm');
	}
	
	// 获取openid
	private function get_openid(){
		$res = $this->wework->get_userinfo_bycode();
		$userid = $res['UserId'];
		if(!$userid){
			$this->show_message('未使用企业微信扫码或非企业成员');
		}
		$res = $this->wework->userid_to_openid($userid);
		$openid = $res['openid'];
		if($openid){
			$this->input->set_cookie('food_openid', $openid, 3600*24*30);
		}else{
			$this->show_message('未获取到openid');
		}
		$this->openid = $openid;
		$this->userid = $userid;
	}
	
	// 今天的上报数据
	private function today_order($openid){
		$time_start = strtotime(date('Y-m-d'));
		$time_end = strtotime(date('Y-m-d 23:59:59'));
		return $this->food_order_model->get_row([
			'openid' => $openid,
			'createtime >=' => $time_start,
			'createtime <=' => $time_end,
		]);
	}
	
	// 验证报餐是否在有效时间内
	private function valid_order_time(){
		$start = strtotime(date('Y-m-d 08:00:00'));
		$end = strtotime(date('Y-m-d 13:00:00'));
		$now = time();
		if($now < $start || $now > $end){
			return false;
		}
		return true;
	}
	
}